From 9629f99dd0a87d4c8fb8953fd88bb8c9db946a9c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 15 Jun 2017 08:08:35 -0700 Subject: [PATCH] Use the `atty` crate for TTY detection This is more robust in the face of msys terminals and otherwise helps share more dependencies! Closes #4166 --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/cargo/core/shell.rs | 31 +++++++++---------------------- src/cargo/lib.rs | 1 + 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01942aad0..4b464177d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,7 @@ name = "cargo" version = "0.21.0" dependencies = [ "advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "bufstream 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "cargotest 0.1.0", "crates-io 0.10.0", @@ -68,6 +69,16 @@ dependencies = [ "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "atty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "backtrace" version = "0.3.2" @@ -886,6 +897,7 @@ dependencies = [ "checksum advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e06588080cb19d0acb6739808aafa5f26bfb2ca015b2b6370028b44cf7cb8a9a" "checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" +"checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159" "checksum backtrace 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "72f9b4182546f4b04ebc4ab7f84948953a118bd6021a1b6a6c909e3e94f6be76" "checksum backtrace-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3a0d842ea781ce92be2bf78a9b38883948542749640b8378b3b2f03d1fd9f1ff" "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" diff --git a/Cargo.toml b/Cargo.toml index d5b4b4254..ca033e571 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ name = "cargo" path = "src/cargo/lib.rs" [dependencies] +atty = "0.2" crates-io = { path = "src/crates-io", version = "0.10" } crossbeam = "0.2" curl = "0.4.6" diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 02161ff24..1dfeb375c 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -1,8 +1,9 @@ use std::fmt; use std::io::prelude::*; -use termcolor::{self, StandardStream, Color, ColorSpec, WriteColor}; +use atty; use termcolor::Color::{Green, Red, Yellow}; +use termcolor::{self, StandardStream, Color, ColorSpec, WriteColor}; use util::errors::CargoResult; @@ -138,29 +139,15 @@ impl Shell { impl ColorChoice { fn to_termcolor_color_choice(&self) -> termcolor::ColorChoice { - return match *self { + match *self { ColorChoice::Always => termcolor::ColorChoice::Always, ColorChoice::Never => termcolor::ColorChoice::Never, - ColorChoice::CargoAuto if isatty() => termcolor::ColorChoice::Auto, - ColorChoice::CargoAuto => termcolor::ColorChoice::Never, - }; - - #[cfg(unix)] - fn isatty() -> bool { - extern crate libc; - - unsafe { libc::isatty(libc::STDERR_FILENO) != 0 } - } - - #[cfg(windows)] - fn isatty() -> bool { - extern crate kernel32; - extern crate winapi; - - unsafe { - let handle = kernel32::GetStdHandle(winapi::STD_ERROR_HANDLE); - let mut out = 0; - kernel32::GetConsoleMode(handle, &mut out) != 0 + ColorChoice::CargoAuto => { + if atty::is(atty::Stream::Stderr) { + termcolor::ColorChoice::Auto + } else { + termcolor::ColorChoice::Never + } } } } diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index e2b39cc92..3f03697d4 100755 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -8,6 +8,7 @@ #[macro_use] extern crate scoped_tls; #[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_json; +extern crate atty; extern crate crates_io as registry; extern crate crossbeam; extern crate curl; -- 2.30.2